//
//  SXTMyMessageHeadView.m
//  8.18项目
//
//  Created by ma c on 16/8/18.
//  Copyright © 2016年 zhang. All rights reserved.
//

#import "SXTMyMessageHeadView.h"
#import <Masonry.h>

@interface SXTMyMessageHeadView ()

@property (nonatomic,strong) UIImageView * headBackImage;
@property (nonatomic,strong) UIButton * loginBtn;
@property (nonatomic,strong) UIButton *landingBtn;
@property (nonatomic,strong) UIImageView * iconImage;
@property (nonatomic,strong) UILabel * userNameLabel;
@property (nonatomic,strong) UILabel * lvLabel;

@end

@implementation SXTMyMessageHeadView

-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.headBackImage];
        [self addSubview:self.loginBtn];
        [self addSubview:self.landingBtn];
        [self addSubview:self.iconImage];
        [self addSubview:self.userNameLabel];
        [self addSubview:self.lvLabel];
    }
    return self;
}

//布局
-(void)layoutSubviews {

    [super layoutSubviews];
    __weak typeof(self) weakSelf = self;
    [_headBackImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf).with.insets(UIEdgeInsetsMake(0, 0, 40, 0));
    }];
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX).offset(-60);
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(45, 23));
    }];
    [_landingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX).offset(60);
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(45, 23));
    }];
    [_iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(75, 75));
        make.centerY.equalTo(weakSelf.headBackImage.mas_centerY);
        make.left.equalTo(weakSelf.mas_left).offset(60);
    }];
    [_userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.iconImage.mas_right).offset(39);
        make.height.equalTo(@16);
        make.right.equalTo(weakSelf.mas_right);
        make.top.equalTo(weakSelf.iconImage.mas_top).offset(12);
    }];
    [_lvLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.iconImage.mas_right).offset(39);
        make.height.equalTo(@16);
        make.right.equalTo(weakSelf.mas_right);
        make.bottom.equalTo(weakSelf.iconImage.mas_bottom).offset(-12);
    }];
    
}

- (void)reloadHeadView{

    NSDictionary *loginDic = [[NSUserDefaults standardUserDefaults]valueForKey:@"ISLOGIN"];
    if (loginDic.count) {
        _loginBtn.hidden = YES;
        _landingBtn.hidden = YES;
        _lvLabel.hidden = NO;
        _iconImage.hidden = NO;
        _userNameLabel.hidden = NO;
        _lvLabel.text = loginDic[@"MemberLvl"];
        _userNameLabel.text = loginDic[@"MemberName"];
    }else{
        _loginBtn.hidden = NO;
        _landingBtn.hidden = NO;
        _lvLabel.hidden =YES;
        _iconImage.hidden = YES;
        _userNameLabel.hidden = YES;
    }
}

#pragma mark - 初始化

-(UIImageView *)headBackImage{

    if (!_headBackImage) {
        _headBackImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"我的背景"]];
    }
    return _headBackImage;

}

- (UIButton *)landingBtn{
    if (!_landingBtn) {
        _landingBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_landingBtn setTitle:@"注册" forState:(UIControlStateNormal)];
        [_landingBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [_landingBtn addTarget:self action:@selector(landingBtnMethod) forControlEvents:(UIControlEventTouchUpInside)];
        _landingBtn.backgroundColor = [UIColor clearColor];
    }
    return _landingBtn;
}

- (UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_loginBtn setTitle:@"登录" forState:(UIControlStateNormal)];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [_loginBtn addTarget:self action:@selector(loginBtnMethod) forControlEvents:(UIControlEventTouchUpInside)];
        _loginBtn.backgroundColor = [UIColor clearColor];
    }
    return _loginBtn;
}

-(UIImageView *)iconImage{

    if (!_iconImage) {
        _iconImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"登录界面微博登录"]];
    }
    return _iconImage;
}

-(UILabel *)userNameLabel{

    if (!_userNameLabel) {
        _userNameLabel = [[UILabel alloc]init];
        _userNameLabel.text = @"用户名";
    }
    return _userNameLabel;
}

- (void)landingBtnMethod{
    if (_landingBlock) {
        _landingBlock();
    }
}
//登陆
- (void)loginBtnMethod {

    if (_loginBtnBlock) {
        _loginBtnBlock();
    }
}
- (UILabel *)lvLabel{
    
    if (!_lvLabel) {
        _lvLabel = [[UILabel alloc]init];
        _lvLabel.text = @"一级";
    }
    return _lvLabel;
}





@end
